<script setup name="name">
import { ref, reactive, watchEffect, defineEmits } from 'vue'
import { GetPickingSaleAPI } from '@/api/productionTube/produAdm/slitting/prepara.js'
import {
  GetFinishedListAPI,
  GetTypeSelectAPI,
  GetAwmateriaListAPI,
  GetAccessoriesAPI,
  GetWasteAPI,
  GetHalfListAPI
} from '@/api/material/common'
const { proxy } = getCurrentInstance()

// 封装 方法 字典查询
const {
  customer_complaints_order_type, // 订单状态
  customer_affiliated_organization //所属机构
} = proxy.useDict('customer_complaints_order_type', 'customer_affiliated_organization')

const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset'])

const props = defineProps({
  isShow: {
    type: Boolean,
    default: false
  },
  dict: {
    type: Array,
    default: () => []
  }
})

const loading = ref(false)

const typeSelect = ref([])
// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  wodDocNum: '', // 发货单号
  materialCode: '', // 产品编码
  type: '1',
  materialName: '' // 产品名称
})

const handleSelect = ref({ id: '' })
const TableData = ref([])
const total = ref(0)
// 勾选数据
const handleSelectionChange = row => {
  // console.log(row, 'row')
  handleSelect.value = row
}

// 是否禁用 保存
const d = computed(() => {
  return handleSelect.value?.id ? false : true
})

// 提交查询
const Submit = () => {
  emit('selectsubmit', handleSelect.value)
}

// 提交
const preservation = () => {
  const Obj = handleSelect.value

  emit('preservation', { ...query, ...Obj })
}

// 获取物料类别
const getMaterialType = async () => {
  query.materialType = ''
  const res = await GetTypeSelectAPI({ condingId: query.type })
  typeSelect.value = res.theDrop
  query.materialType = typeSelect.value[0].value
  getList()
}
// 重置
const reset = () => {
  query.materialName = ''
  query.materialCode = ''
  query.wodDocNum = ''
  getList()
}

// 查询
const getList = async () => {
  loading.value = true
  const res = await GetPickingSaleAPI(query)
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}
getMaterialType()
</script>
<template>
  <p-XiaoheDialog :dialogVisible="props.isShow" title="发货信息" @close="emit('cancel')" width="50%">
    <template #body>
      <div class="app-container">
        <!-- 查询条件 -->
        <el-form :inline="true" label-width="100px">
          <el-form-item label="物料类别">
            <el-select v-model="query.type" placeholder="请选择类别" @change="getMaterialType">
              <el-option
                v-for="(item, index) in [
                  {
                    label: '原料',
                    value: '1'
                  },
                  {
                    label: '辅料',
                    value: '2'
                  },
                  {
                    label: '成品',
                    value: '4'
                  },
                  {
                    label: '半成品',
                    value: '3'
                  }
                ]"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品类型">
            <el-select v-model="query.materialType" placeholder="请选择产品类型" @change="getList">
              <el-option v-for="(item, index) in typeSelect" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="销售发货单号">
            <el-input clearable v-model.trim="query.wodDocNum" placeholder="输入销售发货单号" />
          </el-form-item>
          <el-form-item label="物料编码">
            <el-input clearable v-model.trim="query.materialCode" placeholder="输入产品编码" />
          </el-form-item>
          <el-form-item label="物料名称">
            <el-input clearable v-model.trim="query.materialName" placeholder="输入产品名称" />
          </el-form-item>
        </el-form>

        <el-divider />
        <el-table
          stripe
          border
          v-if="!loading"
          :loading="loading"
          :data="TableData"
          highlight-current-row
          @current-change="handleSelectionChange"
        >
          <el-table-column label="序号" width="80" type="index" />
          <el-table-column width="80">
            <template #default="scope">
              <el-radio-group v-model="handleSelect.id">
                <el-radio :label="scope.row.id" size="large">&nbsp;</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column label="销售发货单号" prop="wodDocNum" width="auto" />
          <el-table-column label="物料编码" prop="materialCode" width="auto" />
          <el-table-column label="物料名称" prop="itemName" width="auto" />
          <el-table-column label="物料规格" prop="itemSpec" width="auto" />
        </el-table>
        <div class="pagination">
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="query.pageNum"
            v-model:limit="query.pageSize"
            @pagination="getList"
          />
        </div>
        <!-- 按钮 -->
        <el-row class="Btn">
          <el-button type="primary" @click="getList">查询列表</el-button>
          <el-button type="info" @click="reset">重置</el-button>
          <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
          <el-button type="info" @click="emit('cancel')">取消</el-button>
        </el-row>
      </div>
    </template>
  </p-XiaoheDialog>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
</style>
